package com.example.dragcontentviewdemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.Process;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.Nullable;

import com.example.dragcontentviewdemo.R;


public class MainActivity extends Activity {
    public static WindowManager windowManager; //test member
    private static final String TAG = "MainActivity";

    private static int onCreateTime = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.main_layout_demo);
        Button btn = findViewById(R.id.main_button);
        windowManager = getWindowManager();

        btn.setOnClickListener(v -> {
            Intent intent = new Intent(getApplicationContext(),ChildActivity.class);
            /*
            issue: NEW_TASK 应该是会查找任务的栈的, 但是为什么总是onCreate新的Activity的呢？
             */

            //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            overridePendingTransition(R.anim.activity_fade_in,R.anim.activity_fade_out);
        });
        btn.setText("onCreateTime: " + onCreateTime++);
        btn.setAllCaps(false); //自动大写关闭
    }

    @Override
    protected void onResume() {
        super.onResume();
        makeToast("onResume" + "主活动进程Pid = " + Process.myPid() + "任务id = " + getTaskId());
    }

    @Override
    protected void onStart() {
        super.onStart();
        makeToast("onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        makeToast("onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        makeToast("onDestroy");
    }

    private void makeToast(String msg){
        StringBuilder sb = new StringBuilder("主活动");
        sb.append(" ").append(msg);
        Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        final int hasNewTaskFlag = intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK;
        if (hasNewTaskFlag != 0) {
            makeToast("newIntent with FLAG_ACTIVITY_NEW_TASK");
        }
    }
}
